package com.hzit.security.handler;

import com.alibaba.fastjson.JSON;
import com.hzit.security.util.ResponseUtil;
import com.hzit.util.R;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 类名：
 * 作者：WF
 * 功能：处理授权异常的类 ( 适用于前后端分离 )：403异常（无权访问）
 */
@Component
public class HzitAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
		// R<Object> result = R.fail(HttpStatus.FORBIDDEN.value(), "无权访问！");
		// String json = JSON.toJSONString(result);
		// response.setContentType("application/json;charset=utf8");
		// response.getWriter().println(json);
		// response.getWriter().flush();
		//403错误
		ResponseUtil.out(response,R.fail(HttpStatus.FORBIDDEN.value(), "无权访问"));
	}
}
